package win.smartown.android.library.scrollpager.impl;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import win.smartown.android.library.scrollpager.interfaces.OnPagerScrollListener;
import win.smartown.android.library.scrollpager.interfaces.Scrollable;

/**
 * Created by Smartown on 2017/9/25.
 */
public class ScrollableScrollView extends ScrollView implements Scrollable {
    public ScrollableScrollView(Context context) {
        super(context);
    }

    public ScrollableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ScrollableScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        onScroll();
        return super.onTouchEvent(ev);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public boolean atTop() {
        return getChildAt(0) == null || getScrollY() == 0;
    }

    @Override
    public boolean atBottom() {
        return getChildAt(0) == null || getChildAt(0).getMeasuredHeight() <= getScrollY() + getHeight();
    }

    @Override
    public void onScroll() {
        if (getParent() instanceof OnPagerScrollListener) {
            ((OnPagerScrollListener) getParent()).onPagerScroll(this, getScrollY());
        }
    }

}
